export default function access(initialState: { currentUser?: API.CurrentUser } | undefined) {
  const { currentUser } = initialState ?? {};
  return {
    canAdmin: currentUser && currentUser.access === 'admin',
    hasRoute(route) {
      const camenu = JSON.parse(localStorage.getItem('camenu') || '[]');
      // 递归遍历菜单树，匹配路由的path或name
      const matchMenu = (menuList: any[]) => {
        return menuList.some(item => {
          // 匹配当前路由的path或name
          const matchPath = item.path === route.path;
          const matchName = item.title === route.name;
          // 若有子菜单，递归校验
          if (item.children && item.children.length > 0) {
            return matchPath || matchName || matchMenu(item.children);
          }
          return matchPath || matchName;
        });
      };
      return matchMenu(camenu);
    },
  };
}